package com.soulmate.user.controlloer;

import common.pojo.User;
import common.utils.CookieUtils;
import common.vo.SysResult;
import com.soulmate.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user/manage")
public class UserController {
    @Autowired
    private UserService userService=null;

    /**
     * 检查用户是否可用
     * @param userName
     * @return
     */
    @RequestMapping("/checkUserName")
    public SysResult checkUserName(String userName){
        try {
            userService.checkUserName(userName);
            return SysResult.ok();

        } catch (Exception e) {
            return SysResult.build(400,"用户已存在！",null);
        }
    }

    /**
     * 新增用户
     * @param user 用户对象
     * @return
     */
    @RequestMapping("/save")
    public SysResult addUserName(User user){
        try {
            userService.addUserName(user);
            return SysResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(400,"新增失败",null);
        }
    }
    /**
     * 登录功能 login 实现登录用户名和密码校验存储到redis中
     */
    @RequestMapping("/login")
    public SysResult login(User user, HttpServletRequest req, HttpServletResponse res){
        /*System.out.println("进入登录。。。");*/
        try {
            //从业务层获取redis的key值，ticket
            String ticket = userService.login(user);
            //判断非空!=null
            if (ticket!=null&&!"".equals(ticket))
            {
                //非空，登录成功。
                //ticket加入到cookie中返回给浏览器
                CookieUtils.setCookie(req,res,"EM_TICKET",ticket);
                return SysResult.ok();
            }
            return SysResult.build(400,"登录失败!",null);
        } catch (Exception e) {
            return SysResult.build(400,"登录失败!",null);
        }
    }
    /**
     * 用户登录状态的获取
     */
    @RequestMapping("/query/{ticket}")
    public SysResult queryUserJson(@PathVariable("ticket") String ticket){
        String userJson=userService.queryUserJson(ticket);
        if (userJson==null){
            return SysResult.build(400,"请求超时",null);
        }
        return SysResult.build(200,"ok",userJson);
    }
    /**
     * 用户登出
     */
    @RequestMapping("/logout")
    public SysResult logout(HttpServletRequest req,HttpServletResponse res){
        //1.干掉cookie
        String EM_TICKET=CookieUtils.getCookieValue(req,"EM_TICKET");
        CookieUtils.setCookie(req,res,"EM_TICKET",null,0);
        //2.调用事务处理层，干掉redis里面的ticket.
        try {
            userService.logout(EM_TICKET);
            return SysResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(400,"登出异常",null);
        }
    }


}
